<?php

class BGoodsOrderReturn extends BaseModel
{
    protected $res;
    protected $key = "admin_";

    public function getSource()
    {
        return "f_b_goods_order_return";
    }

    public function initialize()
    {
        parent::initialize();
    }


    public function order_return_list($params = [])
    {
        $list = [];
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $where = '1=1 ';
        if (isset($params['phone']) && $params['phone']) {
            $where .= ' and u.phone like %' . $params['phone'] . '%';
        }
        if (isset($params['out_trade_no']) && $params['out_trade_no']) {
            $where .= ' and o.out_trade_no =' . $params['out_trade_no'];
        }
        $order = 'r.status asc,r.created_at DESC';
        $sql   = <<<EOT
                SELECT
                    u.real_name,
                    u.phone,
                    o.goods_count,
                    o.goods_amount_total,
                    r.id,
                    r.order_id,
                    r.return_type,
                    r.handling_way,
                    r.return_amount,
                    r.data,
                    r.return_remark,
                    r.remark,
                    r.logistics_code,
                    r.orderlogistics_id,
                    r.status,
                    r.trade_no,
                    r.created_at,
                    r.handling_at 
                FROM
                    SelfGoodsOrderReturn AS r
                    LEFT JOIN SelfGoodsOrder AS o ON r.order_id = o.id
                    LEFT JOIN User AS u ON o.user_id = u.id 
                WHERE
                    {$where}
                ORDER BY
                    {$order}
                    LIMIT {$page},{$size}
                      
EOT;
        /**
         * @see ManagerInterface::executeQuery
         */
        $res = $this->modelsManager->executeQuery($sql);
        if ($res) {
            $list = $res->toarray();
        }

        return $list;
    }


    public function order_return_count($params = [])
    {

        $where = '1=1 ';
        if (isset($params['phone']) && $params['phone']) {
            $where .= ' and u.phone like %' . $params['phone'] . '%';
        }
        if (isset($params['out_trade_no']) && $params['out_trade_no']) {
            $where .= ' and o.out_trade_no =' . $params['out_trade_no'];
        }

        $sql = <<<EOT
                SELECT
                 count(r.id) as total
                FROM
                    SelfGoodsOrderReturn AS r
                    LEFT JOIN SelfGoodsOrder AS o ON r.order_id = o.id
                    LEFT JOIN User AS u ON o.user_id = u.id 
                WHERE
                    {$where}
                limit 1

EOT;

        /**
         * @see ManagerInterface::executeQuery
         */
        $res = $this->modelsManager->executeQuery($sql);

        $list = [];
        if ($res) {
            $list = $res->toarray();
        }

        return isset($list[0]['total']) ? $list[0]['total'] : 0;;
    }

    public function order_return_details($params = '1=1',$columns='*')
    {
        $list    = $this::findFirst(
            array(
                $params,
                'columns' => $columns,
            ));
        $result  = [];
        if ($list) {
            $result = $list->toarray();
        }
        return $result;
    }




}
